$(function () {
	// 返回按钮
	$("#goback").click(function () {
		location.href ='index3.html';
	});
	// 设置按钮
	$(".settings").on("click", function () {
		$(".settingsC").slideToggle();
	});
	var timer = "";
	var totalHeight = $("#reply")[0].scrollHeight + 44 + $("#themeSection").height();

	// 回到顶部事件
	$("#backTop").click(function (event) {
		$("html,body").animate({
			scrollTop: 0
		}, 500)
	});
	// 到底部按钮事件
	$("#backBottom").click(function () {
		var vh = $("#reply")[0].scrollHeight + $("#themeSection").height() - $(window).height() + 44;
		$("html,body").animate({
			scrollTop: vh
		}, 500);
	});
	/**********************请求***********************************/
	var dataCount = 0;
	var themeHtml = "";
	var replyHtml = "";
	var sectionId = "";
	var data = {
		id: getQueryVariable('id')
	};
	// 发送获取留言请求
	$.ajax({
		type: "get",
		url: "/theme",
		async: true,
		data: data
	}).done(function (resData, status, jqXHR) {
		themeHtml = template("themeSection1", resData.theme);
		$("#themeSection").html(themeHtml);
		sectionId = resData.theme.id;
		if (resData.reply) {
			replyHtml = template("replyContainer", resData);
			// console.log(replyHtml);
			$("#reply").html(replyHtml);
			dataCount = resData.reply.length;
			$(".dataCount").text(dataCount);
		}
		if (resData.authorId == $.cookie("wcID")) {
			$(".settings").fadeIn();
			$(".deleteReply").show().parent().css("width", 230);
		}
	}).fail(function (jqXHR, status, error) {
		console.log(error);
	});
	var favaCount = "";
	// 点赞数加1
	$("#themeSection").on("click", ".likeWC", function () {
		var arg = arguments;
		favaCount = $(this).next().text();
		if (!$(this).data("like")) {
			favaCount++;
			$(this).removeClass("glyphicon-heart-empty").addClass("glyphicon-heart").next().text(favaCount);
			$(this).data("like", true);
		} else {
			favaCount--;
			$(this).addClass("glyphicon-heart-empty").removeClass("glyphicon-heart").next().text(favaCount);
			$(this).data("like", false);
		}
	});


	// 顶部的删除按钮事件
	$("header").on("click", ".deleteComment", function () {
		if (confirm("是否删除所有")) {
			$.ajax({
				type: "post",
				url: "/deleteComment",
				data: sectionId,
				async: true
			}).done(function (status, resData, jqXHR) {
				if (resData.code == 1) {
					alert("删除成功");
					$("#reply").empty();
				}
			}).fail(function (jqXHR, status, error) {
				alert("删除失败");
			});
		}

	});
	// 点击 评论的事件
	$("#reply").on("click", "li", function () {
		$(this).addClass("animated flipInX").find(".guard").fadeIn(1000);
	});
	// 点击 取消按钮事件
	$("#reply").on("click", ".gCancel", function (event) {
		event.stopImmediatePropagation();
		$(this).parents("li").removeClass("animated flipInX").find(".guard").fadeOut(1000);
	});
	// 点击回复按钮事件
	$("#reply").on("click", ".gReply", function (event) {
		event.stopImmediatePropagation();
		if (!$.cookie("wcID")) {
			if (confirm("您尚未登陆是否为您跳转到登陆界面")) {
				location.href = "register2.html";
				$(this).parents("li").removeClass("animated flipInX").find(".guard").fadeOut(1000);
			}
		} else {
			var com_id = $(this).parents("li").find(".sectionID").text();
			var authorId = $(this).parents("li").find(".authorId").text();
			var theme_id = $('#themeSection').find(".sectionID").text();
			console.log(com_id);
			console.log(theme_id);
			// var obj = {
			// 	data,
			// 	userId: $.cookie("wcID"),
			// 	authorId
			// };
			// $.get("/replySection", obj, function () {});
			location.href = '/reply?com_id=' + com_id + '&id=' + theme_id;
		}
	});
	// 点击删除按钮事件
	$("#reply").on("click", ".deleteReply", function (event) {
		event.stopImmediatePropagation();
		var data = $(this).parents("li").find(".sectionID").text();
		var obj = {
			sectionId,
			authorId: data
		}
		var $this = $(this).parents("li");
		$.get("/deleteReply", obj, function (resData) {
			if (resData.code == 1) {
				$this.fadeOut(600);
			}
		});
	});
	// 上拉加载
	$(window).scroll(function () {
		clearTimeout(timer);
		$(".shortcutBtn").fadeIn();
		timer = setTimeout(function () {
			$(".shortcutBtn").fadeOut();
		}, 1000);
		var vx = $(window).scrollTop();
		var vh = $(window).height();
		var total = vx + vh;
		if (total >= totalHeight - 10) {
			$(".loading").css({
				backgroundImage: "url(../images/loading_010.gif)",
				backgroundRepeat: "no-repeat",
				backgroundPosition: "50% 0"
			}).find("div").hide();
			var data = {
				dataCount,
				sectionId
			}
			$.ajax({
				type: "get",
				url: "/newReply",
				async: true,
				data: data
			}).done(function (resData) {
				if (resData) {
					htmlStr = template("mainTemplate", resData);
					$("#reply").append(htmlStr);
					totalHeight = $("#reply")[0].scrollHeight + 44 + $("#themeSection").height();
					dataCount += dataCount + resData.length;
					$(".dataCount").text(dataCount);
					$(".loading").css("background-image", "").find("div").show().find(".iconM").show().next().text("上拉加载");
				} else {
					$(".loading").css("background-image", "").find("div").show().html("<p>没有更多的数据</p>");
				}
			}).fail(function (jqXHR, status, error) {
				$(".loading").css("background-image", "").find("div").show().html("<p><span class='glyphicon glyphicon-remove'></span>&nbsp;加载失败</p>");
			})
		}
	});

	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
		return (false);
	}

	$('#themeSection').on('click','.readWC',function(){
		location.href = '/reply?id=' + sectionId;
	})

});


window.onbeforeunload = function () {
	if ($(".likeWC").data("like")) {
		$.post("/section", favaCount, function () {});
	};

}